import { DynamicModule, Global, Module } from '@nestjs/common';
import { PostgresqlPersistence } from 'y-postgresql';

@Global()
@Module({})
export class YjsPostgresqlModule {
  static forRoot(): DynamicModule {
    return {
      module: YjsPostgresqlModule,
      providers: [
        {
          provide: 'YJS_POSTGRESQL_ADAPTER',
          useFactory: async () => {
            const pgdb = await PostgresqlPersistence.build(
              {
                host: 'localhost',
                port: 5433,
                database: 'postgres',
                user: 'postgres',
                password: '123456',
              },
              { tableName: 'yjs-writings', useIndex: false, flushSize: 200 },
            );
            return pgdb;
          },
        },
      ],
      exports: ['YJS_POSTGRESQL_ADAPTER'],
    };
  }
}
